package com.hjb.controller;

import com.hjb.common.Response;
import com.hjb.config.CustomProperty;
import com.hjb.config.DynamicDatasourceHelper;
import com.hjb.config.TestProperty;
import com.hjb.entity.Database;
import com.hjb.service.IDatabaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 数据库操作
 * @author HuJiaBing
 * @date 2025/7/4 9:16
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/db")
public class DatabaseController {

    @Resource
    private DynamicDatasourceHelper dynamicDatasourceHelper;
    @Resource
    private IDatabaseService databaseService;
    @Resource
    private CustomProperty customProperty;
    @Resource
    private TestProperty testProperty;

    @GetMapping("/list")
    public Response<List<Database>> listDatabase() {
        return Response.success(databaseService.listAll());
    }
    @PostMapping("/save")
    public Response<Boolean> addDatabase(@RequestBody Database db) {
        databaseService.saveDatabase(db);
        return Response.success(true);
    }
    @PostMapping("/test")
    public Response<String> testConnection(@RequestBody Database db) {
        boolean connectionStatus = dynamicDatasourceHelper.testConnection(db);
        return Response.success(connectionStatus? "连接成功" : "连接失败");
    }

    @DeleteMapping("/delete")
    public Response<Boolean> deleteDatabase(@RequestBody Database db) {
        databaseService.deleteDatabase(db.getId());
        return Response.success(true);
    }
    @GetMapping("/tables")
    public Response<Map<String, String>> getTables(@RequestParam("id") String id) {
        return Response.success(dynamicDatasourceHelper.getTables(id));
    }

    @GetMapping("/tableData")
    public Response<Object> getTableData(@RequestParam("id") String id, @RequestParam("tableName") String tableName) {
        return Response.success(true);
    }

    @GetMapping("/listInDiff")
    public Response<List<Database>> listInDiff() {
        return Response.success(databaseService.listDiff());
    }

    @PostMapping("/txUpdate")
    public Response<Boolean> updateDatabase() {
        databaseService.updateDatabase();
        return Response.success(true);
    }

    @GetMapping("/test")
    public Response<Boolean> getInfo() {
        log.error("日志：{}", customProperty);
        log.error("日志：{}", testProperty);
        return Response.success(true);
    }

}
